package edu.chl.jadetalk.control;

import edu.chl.jadetalk.user.UserSessionBean;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.primefaces.event.RateEvent;

/**
 *
 * @author Erik
 */
@RequestScoped
@ManagedBean
public class RatingControllerBean {

    @ManagedProperty(value = "#{userSessionBean}")
    private UserSessionBean userSessionBean;
    private double rate;

    public void handleRate(RateEvent rateEvent) {
        UIComponent ratingComponent = rateEvent.getComponent();
        long entryId = (Long) ratingComponent.getAttributes().get("entryId");

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, 
                "Rate Event", "You rated:" + (int) rateEvent.getRating());
        FacesContext.getCurrentInstance().addMessage(null, message);
        rate = rateEvent.getRating();
        Logger.getAnonymousLogger().log(Level.INFO,
                String.format("'%s' entered a rating of %.0f for image with entryId %d",
                userSessionBean.getUser().getUsername(),
                rateEvent.getRating(),
                entryId));
    }

    public void setRating(int rate) {
        this.rate = rate;
        Logger.getAnonymousLogger().log(Level.INFO, "You just successfully starred that entry! Star value: " + rate);
    }

    public double getRating() {
        return rate;
    }
}
